home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / managers / git-4.3 / git-4 / git-4.3.7 / src / xio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-08  |  4.3 KB  |  252 lines

  1. /* xio.c -- safe versions of the system io primitives. It also includes a
  2.    new version of the readlink system call that computes the number of
  3.    characters required to hold the entire link. */
  4.  
  5. /* Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
  6.  
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2, or (at your option)
  10.    any later version.
  11.  
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* Written by Tudor Hulubei and Andrei Pitis.  */
  22.  
  23.  
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27.  
  28. #include <sys/types.h>
  29.  
  30. #ifdef HAVE_UNISTD_H
  31. #include <unistd.h>
  32. #endif /* HAVE_UNISTD_H */
  33.  
  34. #ifndef HAVE_GETCWD
  35. #include <sys/param.h>
  36. #ifdef MAXPATHLEN
  37. #define MAXPATHSIZE MAXPATHLEN
  38. #else
  39. #ifdef PATH_MAX
  40. #define MAXPATHSIZE PATH_MAX
  41. #else
  42. #define MAXPATHSIZE 1024
  43. #endif /* PATH_MAX */
  44. #endif /* MAXPATHLEN */
  45. #endif /* HAVE_GETCWD */
  46.  
  47. #include <errno.h>
  48.  
  49. /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
  50. #if !defined (errno)
  51. extern int errno;
  52. #endif /* !errno */
  53.  
  54.  
  55. #include "xmalloc.h"
  56. #include "xio.h"
  57.  
  58.  
  59. int
  60. xread(fd, buf, count)
  61.     int fd;
  62.     char *buf;
  63.     size_t count;
  64. {
  65.     int chars;
  66.     int old_errno;
  67.  
  68.     if (count <= 0)
  69.         return count;
  70.  
  71. #ifdef EINTR
  72.     old_errno = errno;
  73.  
  74.     do
  75.         chars = read(fd, buf, count);
  76.     while (chars < 0 && errno == EINTR);
  77.  
  78.     errno = old_errno;
  79.     return chars;
  80. #else
  81.     return read(fd, buf, count);
  82. #endif
  83. }
  84.  
  85.  
  86. int
  87. xwrite(fd, buf, count)
  88.     int fd;
  89.     char *buf;
  90.     size_t count;
  91. {
  92.     int chars;
  93.     int old_errno;
  94.  
  95.     if (count <= 0)
  96.         return count;
  97.  
  98. #ifdef EINTR
  99.     old_errno = errno;
  100.  
  101.     do
  102.         chars = write(fd, buf, count);
  103.     while (chars < 0 && errno == EINTR);
  104.  
  105.     errno = old_errno;
  106.     return chars;
  107. #else
  108.     return write(fd, buf, count);
  109. #endif
  110. }
  111.  
  112.  
  113. int
  114. __xreadlink(path, buf, size)
  115.     const char *path;
  116.     char *buf;
  117.     size_t size;
  118. {
  119.     int chars;
  120.     int old_errno;
  121.  
  122.     if (size <= 0)
  123.         return size;
  124.  
  125. #ifdef EINTR
  126.     old_errno = errno;
  127.  
  128.     do
  129.     {
  130.         chars = readlink(path, buf, size);
  131.         old_errno = size;
  132.     }
  133.     while (chars < 0 && errno == EINTR);
  134.  
  135.     errno = old_errno;
  136.     return chars;
  137. #else
  138.     return read(fd, buf, count);
  139. #endif
  140. }
  141.  
  142.  
  143. int
  144. xreadlink(filename)
  145.     const char *filename;
  146. {
  147.     int size = 100;
  148.  
  149.     for (;;)
  150.     {
  151.         char *buffer = xmalloc(size);
  152.         int  nchars  = __xreadlink(filename, buffer, size);
  153.  
  154.         if (nchars < size)
  155.         {
  156.             xfree(buffer);
  157.             return nchars;
  158.         }
  159.  
  160.         xfree(buffer);
  161.         size *= 2;
  162.     }
  163. }
  164.  
  165.  
  166. int
  167. xfstat(filedes, buf)
  168.     int filedes;
  169.     struct stat *buf;
  170. {
  171.     int result;
  172.  
  173.     do
  174.         result = fstat(filedes, buf);
  175.     while (result < 0 && errno == EINTR);
  176.  
  177.     return result;
  178. }
  179.  
  180.  
  181. int
  182. xstat(filename, buf)
  183.     const char *filename;
  184.     struct stat *buf;
  185. {
  186.     int result;
  187.  
  188.     do
  189.         result = stat(filename, buf);
  190.     while (result < 0 && errno == EINTR);
  191.  
  192.     return result;
  193. }
  194.  
  195.  
  196. int
  197. xlstat(filename, buf)
  198.     const char *filename;
  199.     struct stat *buf;
  200. {
  201.     int result;
  202.  
  203.     do
  204. #ifdef HAVE_LSTAT
  205.         result = lstat(filename, buf);
  206. #else
  207.         result = stat(filename, buf);
  208. #endif /* HAVE_LSTAT */
  209.     while (result < 0 && errno == EINTR);
  210.  
  211.     return result;
  212. }
  213.  
  214.  
  215. char *
  216. xgetcwd()
  217. {
  218.     size_t size;
  219.     char *result;
  220.     char *cwd;
  221.  
  222.     errno = 0;
  223.  
  224. #ifdef HAVE_GETCWD
  225.     cwd = xmalloc(size = 64);
  226.  
  227.     while ((result = getcwd(cwd, size)) == NULL && errno == ERANGE)
  228.     {
  229.         cwd = xrealloc(cwd, size += 64);
  230.         errno = 0;
  231.     }
  232. #else
  233.     /* No getcwd() -> getwd(): BSD style... bleah!  */
  234.  
  235.     cwd    = xmalloc(MAXPATHSIZE + 2);
  236.     result = getwd(cwd);
  237.  
  238.     if (result)
  239.         cwd = xrealloc(cwd, strlen(cwd) + 1);
  240. #endif  /* HAVE_GETCWD */
  241.  
  242.     if (result == NULL)
  243.     {
  244.         int old_errno = errno;
  245.         xfree(cwd);
  246.         errno = old_errno;
  247.         return NULL;
  248.     }
  249.  
  250.     return cwd;
  251. }
  252.